package com.course.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.course.api.config.Result.Result;
import com.course.api.config.Result.Results;
import com.course.api.entity.Course;
import com.course.api.service.Impl.CourseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/course")
public class CourseController {
    @Autowired
    CourseServiceImpl courseService;

    /**
     * 获取课程分页数据
     * @param page
     * @param pageSize
     * @param teacherId
     * @return
     */
    @GetMapping("/getCourseList")
    public Result getCourseList(@RequestParam int page,
                                @RequestParam int pageSize,
                                @RequestParam(defaultValue = "0") Long teacherId) {
        IPage<Course> courseIPage = new Page<>(page, pageSize);
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        if (teacherId > 0) {
            queryWrapper.eq("teacher_id", teacherId);
        }
        return Results.success(courseService.page(courseIPage, queryWrapper));
    }

    /**
     * 获取全部课程信息
     * @return
     */
    @GetMapping("/getAllCourseList")
    public Result getAllCourseList() {
        return Results.success(courseService.list());
    }

    /**
     * 添加课程
     * @param course
     * @return newCourseId
     */
    @PostMapping("/add")
    public Result add(@RequestBody Course course) {
        courseService.save(course);
        List<Course> courseList = courseService.list();
        System.out.println(courseList.get(courseList.size() - 1).getId());
        return Results.success(courseList.get(courseList.size() - 1).getId());
    }

    /**
     * 修改课程信息
     * @param course
     * @return
     */
    @PutMapping("update")
    public Result update(@RequestBody Course course) {
        if (courseService.updateById(course)) return Results.success();
        else return Results.failure("编辑失败");
    }

    /**
     * 删除课程
     * @param id
     * @return
     */
    @DeleteMapping("delete/{id}")
    public Result remove(@PathVariable(value = "id") Long id) {
        if (courseService.removeById(id)) return Results.success();
        else return Results.failure("删除失败");
    }

    /**
     * 选课
     * @param courseId
     * @param userId
     * @return
     */
    @PutMapping("/selectCourse/{courseId}/{userId}")
    public Result selectCourse(@PathVariable(value = "courseId") Long courseId,
                               @PathVariable(value = "userId") Long userId) {
        courseService.selectCourseById(courseId, userId);
        return Results.success();
    }

    /**
     * 退选
     * @param courseId
     * @param userId
     * @return
     */
    @PutMapping("deselectCourse/{courseId}/{userId}")
    public Result deselectCourse(@PathVariable(value = "courseId") Long courseId,
                                 @PathVariable(value = "userId") Long userId) {
        courseService.deselectCourseById(courseId, userId);
        return Results.success();
    }
}
